﻿using BLL;
using BLL.Comm;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Inventory.sontemplate
{
    public partial class _Supplier : Inventory.template.BaseInfoForm
    {
        private Supplier c;
        private Contact contact;
        private SupplierBLL bl;
        private List<Supplier> list;
        public _Supplier()
        {
            InitializeComponent();
            bl = new SupplierBLL();
            dgvGhxx.AutoGenerateColumns = false;//使清空DataGridView控件数据源时，标题内容不会被清空
            BindData();//绑定DataGridView的数据源
            ControlOpera(false);
        }
        private void BindData()
        {
            dgvGhxx.DataSource = null;//清空DataGridView的数据源
            list = bl.GetList();
            dgvGhxx.DataSource = list;//将list集合数据绑定到控件dgvCustomer作为数据源
        }
        /// <summary>
        /// 定义单击 “新增/修改/删除”按钮式，GroupBox控件中TextBox的可操作性
        /// </summary>
        /// <param name="value"></param>
        private void ControlOpera(bool value)
        {
            tagGtxtKhbh.Enabled = value;
            tagGtxtKhnc.Enabled = value;
            tagGtxtDz.Enabled = value;
            tagGtxtYb.Enabled = value;
            tagGtxtDh.Enabled = value;
            tagGtxtCz.Enabled = value;
            tagGtxtKhyh.Enabled = value;
            tagGtxtYhzh.Enabled = value;
            tagGtxtLxr.Enabled = value;
            tagGtxtDzyj.Enabled = value;
        }
        

        private void tagbtnXz_Click(object sender, EventArgs e)
        {
            tagGtxtPy.Enabled = false;
            ControlOpera(true);
        }
        /// <summary>
        /// 将业务实体内容显示到TextBox控件中
        /// </summary>
        private void ShowInfo()
        {
            tagGtxtKhbh.Text = dgvGhxx.CurrentRow.Cells["SupplierID"].Value.ToString();
            tagGtxtKhnc.Text = dgvGhxx.CurrentRow.Cells["SupplierName"].Value.ToString();
            tagGtxtPy.Text = dgvGhxx.CurrentRow.Cells["SpellingCode"].Value.ToString();
            tagGtxtDz.Text = dgvGhxx.CurrentRow.Cells["Address"].Value.ToString();
            tagGtxtYb.Text = dgvGhxx.CurrentRow.Cells["ZipCode"].Value.ToString();
            tagGtxtDh.Text = dgvGhxx.CurrentRow.Cells["Tel"].Value.ToString();
            tagGtxtCz.Text = dgvGhxx.CurrentRow.Cells["Fax"].Value.ToString();
            tagGtxtKhyh.Text = dgvGhxx.CurrentRow.Cells["BankName"].Value.ToString();
            tagGtxtYhzh.Text = dgvGhxx.CurrentRow.Cells["BankAccount"].Value.ToString();
            tagGtxtLxr.Text = dgvGhxx.CurrentRow.Cells["Contacter"].Value.ToString();
            tagGtxtDzyj.Text = dgvGhxx.CurrentRow.Cells["Email"].Value.ToString();
        }
        /// <summary>
        /// 修改 按钮单击事件
        /// </summary>
        private void tagbtnXg_Click(object sender, EventArgs e)
        {
            ControlOpera(true);
            tagGtxtPy.Enabled = false;
            tagGtxtKhbh.Enabled = false;

            ShowInfo();
        }
        /// <summary>
        /// 删除 按钮单击事件
        /// </summary>
        private void tagbtnSc_Click(object sender, EventArgs e)
        {
            ControlOpera(false );
            tagGtxtPy.Enabled = false;
            ShowInfo();
        }
        /// <summary>
        /// 文本框输入内容数据验证
        /// </summary>
        private string Validation()
        {
            if (tagGtxtKhbh.Text.Trim() == "") { tagGtxtKhbh.Focus(); return "编号不能为空值！"; }
            if (tagGtxtKhnc.Text.Trim() == "") { tagGtxtKhnc.Focus(); return "名称不能为空值！"; }
            if (tagGtxtDz.Text.Trim() == "") { tagGtxtDz.Focus(); return "地址不能为空值！"; }
            if (tagGtxtYb.Text.Trim() == "") { tagGtxtYb.Focus(); return "邮编不能为空值！"; }
            if (tagGtxtDh.Text.Trim() == "") { tagGtxtDh.Focus(); return "联系方式不能为空值！"; }
            if (tagGtxtCz.Text.Trim() == "") { tagGtxtCz.Focus(); return "传真不能为空值！"; }
            if (tagGtxtKhyh.Text.Trim() == "") { tagGtxtKhyh.Focus(); return "银行名称不能为空值！"; }
            if (tagGtxtYhzh.Text.Trim() == "") { tagGtxtYhzh.Focus(); return "银行账号不能为空值！"; }
            if (tagGtxtLxr.Text.Trim() == "") { tagGtxtLxr.Focus(); return "联系人不能为空值！"; }
            if (tagGtxtDzyj.Text.Trim() == "") { tagGtxtDzyj.Focus(); return "Email不能为空值！"; }
            return "";
        }
        /// <summary>
        /// 获取TextBox控件内容，并完成Customer实例化
        /// </summary>
        private Supplier SetValue()
        {
            c = new Supplier();
            c.SupplierID  = tagGtxtKhbh.Text.Trim();
            c.SupplierName  = tagGtxtKhnc.Text.Trim();

            c.SpellingCode = tagGtxtPy.Text.Trim();
            c.Address = tagGtxtDz.Text.Trim();
            c.ZipCode = tagGtxtYb.Text.Trim();
            c.Tel = tagGtxtDh.Text.Trim();
            c.Fax = tagGtxtCz.Text.Trim();
            c.BankName = tagGtxtKhyh.Text.Trim();
            c.BankAccount = tagGtxtYhzh.Text.Trim();
            c.Contacter = tagGtxtLxr.Text.Trim();
            c.Email = tagGtxtDzyj.Text.Trim();

            return c;
        }
        /// <summary>
        /// 存盘 按钮单击事件
        /// </summary>
        private void tagbtnCp_Click(object sender, EventArgs e)
        {
            string errorString = Validation();
            if (errorString.Length > 0) { MessageBox.Show(errorString, "系统提示"); return; }
            if (opera == Operation.INSERT)
            {
                SetValue();//获取控件值并完成对象赋值
                if (bl.Insert(c))
                {
                    BindData();//绑定DataGridView的数据源

                    Clear();//清空TextBox控件内容
                    MessageBox.Show("新增数据成功！", "系统提示");
                }
                else
                    MessageBox.Show("新增数据失败！", "系统提示");
            }
            if (opera == Operation.UPDATE)
            {
                if (c != null)
                {
                    SetValue();//获取控件值并完成对象赋值
                    if (bl.Update(c))
                    {
                        BindData();//绑定DataGridView的数据源
                        ShowInfo();
                        Clear();//清空TextBox控件内容
                        MessageBox.Show("成功更新数据！", "系统提示");
                    }
                    else
                        MessageBox.Show("新增数据失败！", "系统提示");
                }
                else
                    MessageBox.Show("请选择需要修改的记录", "系统提示");
            }
            if (opera == Operation.DELETE)
            {
                if (c != null)
                {
                    SetValue();//获取控件值并完成对象赋值
                    if (bl.Delete(c))
                    {
                        BindData();//绑定DataGridView的数据源

                        Clear();//清空TextBox控件内容
                        MessageBox.Show("成功删除数据！", "系统提示");
                    }
                    else
                        MessageBox.Show("删除数据失败！", "系统提示");
                }
                else
                    MessageBox.Show("请选择需要删除的记录", "系统提示");
            }
        }

        private void dgvGhxx_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvGhxx .CurrentRow.Selected = true;
            c = list[e.RowIndex];
            if (opera == Operation.UPDATE || opera == Operation.DELETE)
                ShowInfo();
        }

        private void tagGtxtKhnc_TextChanged(object sender, EventArgs e)
        {
            tagGtxtPy.Text = PY.HZToSpell(tagGtxtKhnc.Text.Trim());
        }
    }

}
